capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local crime "";
local output "";

/* 
Table B5
Placebo tests
*/

local fs_vars  "age18_yes n_age18unfav" ;
local covariates "n_male n_firstage n_fam_singmom n_fam_nopar n_dibmdr_mie n_dibmdr_mine n_dibmdr_miss n_diag1_mental n_diag1_nerv n_diag1_sense n_diag1_infec n_diag1_endo n_diag1_none n_diag1_cong n_diag1_musc n_diag1_resp n_diag1_blood n_diag1_neo n_earnhhpre9095" ;

local controls_all "n_male i.n_firstage i.n_diag1cat n_dibmdr_mie n_dibmdr_mine n_dibmdr_miss n_fam_singmom n_fam_nopar i.n_earnhhbins i.state" ;

local bwidth_list "264" ;
local cov_list   "all" ;
local demo_main "all" ;

foreach demo in `demo_main' { ;
	local vars_`demo' "n_crime*" ; 
} ;

local poly_list "1(1)1" ;

use "`crime'\prep2\crime_prwora_prepforreg_exactage.dta", clear ;

/* Create placebo variables */
forval yr = 94(1)98 { ;
	gen dobdd_run`yr'	= dobdd_stata - mdy(08,22,19`yr') ;
	gen dobwk_run`yr'	= dobwk_stata - wofd(mdy(08,22,19`yr')) ;
	gen dobmo_run`yr'	= dobmo_stata - mofd(mdy(08,22,19`yr')) ;
	gen byte n_post`yr' = (dobdd_run`yr' > 0) ;
} ;

foreach yr in 94 95 96 97 { ;

	/* RD variables */
	gen dobdd_run`yr'2=dobdd_run`yr'*dobdd_run`yr';
	gen dobdd_run`yr'3=dobdd_run`yr'2*dobdd_run`yr';
	gen dobdd_run`yr'4=dobdd_run`yr'3*dobdd_run`yr';

	gen postXdobdd`yr'=n_post`yr'*dobdd_run`yr';
	gen postXdobdd`yr'2=n_post`yr'*dobdd_run`yr'2;
	gen postXdobdd`yr'3=n_post`yr'*dobdd_run`yr'3;
	gen postXdobdd`yr'4=n_post`yr'*dobdd_run`yr'4;

	local poly`yr'_1 "dobdd_run`yr' postXdobdd`yr'";
	local poly`yr'_2 "dobdd_run`yr' dobdd_run`yr'2 postXdobdd`yr' postXdobdd`yr'2";
	local poly`yr'_3 "dobdd_run`yr' dobdd_run`yr'2 dobdd_run`yr'3 postXdobdd`yr' postXdobdd`yr'2 postXdobdd`yr'3";
	local poly`yr'_4 "dobdd_run`yr' dobdd_run`yr'2 dobdd_run`yr'3 dobdd_run`yr'4 postXdobdd`yr' postXdobdd`yr'2 postXdobdd`yr'3 postXdobdd`yr'4";
} ;

gen demo_all = 1 ;

foreach yr in 94 95 96 97 { ;

	local exp "prworaplacebo`yr'";
	
	preserve ;
	keep if abs(dobdd_run`yr') <= `bwidth_list' ;

	foreach demo in all { ;

	foreach bwidth in `bwidth_list' {;

		forval poly = `poly_list' {;
			
			foreach cov in `cov_list' { ;
			
					
					/* RD regressions */
					reg n_post`yr' n_post`yr' `poly`yr'_`poly'' 
						if abs(dobdd_run`yr')<=`bwidth' & demo_`demo' == 1 
						, robust;
						outreg2 using "`output'\\`exp'_bw`bwidth'_crime_poly`poly'`cov'cov_rd_`demo'.xls", e(N r2 rmse) replace;
				
					foreach var of varlist 
						`fs_vars' 
						{;
						xi: reg `var' n_post`yr' `poly`yr'_`poly''
							`controls_`cov''
							if abs(dobdd_run`yr')<=`bwidth' & demo_`demo' == 1 & n_crime2char_18_38 != .
							, robust; 
						outreg2 using "`output'\\`exp'_bw`bwidth'_crime_poly`poly'`cov'cov_rd_`demo'.xls", e(N r2 rmse) append;
						} ;	
				
					foreach var of varlist 
						`vars_`demo''
						{;
						xi: reg `var' n_post`yr' `poly`yr'_`poly''
							`controls_`cov''
							if abs(dobdd_run`yr')<=`bwidth' & demo_`demo' == 1
							, robust; 
						outreg2 using "`output'\\`exp'_bw`bwidth'_crime_poly`poly'`cov'cov_rd_`demo'.xls", e(N r2 rmse) append;
						} ;	
					
					
				} ; /* end cov */
					
			} ; /* end poly */
		} ; /* end bwidth*/
	} ;	/* end demo */
	
	restore ;
} ; /* end yr */

capture log close;
